package jschool.ui.Colegio;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import jschool.entidades.Colegio;
import jschool.manejadores.ManejadorDeColegio;
import jschool.manejadores.ManejadorDeIdioma;
import jschool.util.CampoVacioException;
import jschool.util.PintaImagen;
import jschool.util.Utilidad;

public class PanelColegio extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel lblNombre = null;
	private JTextField txtNombre = null;
	private JTabbedPane pestaña = null;
	private JPanel panelDireccion = null;
	private JPanel panelDireccion1 = null;
	private JLabel lblCalle = null;
	private JLabel lblNumero = null;
	private JLabel lblSector = null;
	private JLabel lblMunicipio = null;
	private JTextField txtCalle = null;
	private JTextField txtNumero = null;
	private JTextField txtSector = null;
	private JTextField txtMunicipio = null;
	private JPanel panelContacto = null;
	private JLabel lblTelefono1 = null;
	private JLabel lblFax = null;
	private JLabel lblweb = null;
	private JTextField txtTelefono1 = null;
	private JLabel lblTelefono2 = null;
	private JTextField txtTelefono2 = null;
	private JTextField txtFax = null;
	private JLabel lblEmail = null;
	private JTextField txtEmail = null;
	private JTextField txtWeb = null;
	private JButton btnCancelar = null;
	private JButton btnAceptar = null;

	private JPanel panelOtros = null;
	private JPanel panelOtros1 = null;
	private JPanel panelLogo = null;
	private JButton btnLogo = null;
	private File file = null; // @jve:decl-index=0:
	private ManejadorDeColegio manejador = null; // @jve:decl-index=0:
	private JLabel lblNotas = null;
	private String[] direcciones = null;
	private ManejadorDeIdioma manejadorDeIdioma = null; // @jve:decl-index=0:
	private String[] pestañaNombre = null;
	private JButton btnCambiarIdioma = null;

	public PanelColegio() {
		super();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "La Clase no fue Encontrada",
					"Error", JOptionPane.ERROR_MESSAGE, null);
		} catch (InstantiationException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"Hubo un error en la instanciacion", "Error",
					JOptionPane.ERROR_MESSAGE, null);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Acceso Ilegal a la Clase",
					"Error", JOptionPane.ERROR_MESSAGE, null);
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Estilo no Soportado", "Error",
					JOptionPane.ERROR_MESSAGE, null);
		}

		initialize();
	}

	private void initialize() {
		pestañaNombre = new String[] { "Informacion General", "Otros" };
		manejador = ManejadorDeColegio.getInstancia();
		manejadorDeIdioma = ManejadorDeIdioma.getInstancia();
		file = new File(manejador.getColegio().get(0).getLogo());
		direcciones = manejador.getColegio().get(0).getDireccion().split(",");
		lblNombre = new JLabel();
		lblNombre.setBounds(new Rectangle(13, 12, 133, 18));
		lblNombre.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNombre.setText("Nombre del Colegio:");
		this.setSize(485, 370);
		this.setLayout(null);
		this.setBackground(new Color(212, 208, 200));
		this.add(lblNombre, null);
		this.add(getTxtNombre(), null);
		this.add(getPestaña(), null);
		this.add(getBtnCancelar(), null);
		this.add(getBtnAceptar(), null);
		this.add(getBtnCambiarIdioma(), null);
		PanelColegio.this.cargarImagen(getPanelLogo(), file);

	}

	private JTextField getTxtNombre() {
		if (txtNombre == null) {
			txtNombre = new JTextField();
			txtNombre.setBounds(new Rectangle(151, 12, 330, 20));
			txtNombre.setText(manejador.getColegio().get(0).getNombre());
			txtNombre.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		}
		return txtNombre;
	}

	private JTabbedPane getPestaña() {
		if (pestaña == null) {
			pestaña = new JTabbedPane();
			pestaña.setBounds(new Rectangle(8, 40, 469, 294));
			pestaña.setFont(new Font("Courier New", Font.PLAIN, 12));
			pestaña.add(pestañaNombre[0], getPanelDireccion());
			pestaña.add(pestañaNombre[1], getPanelOtros());
		}
		return pestaña;
	}

	private JPanel getPanelDireccion() {
		if (panelDireccion == null) {
			panelDireccion = new JPanel();
			panelDireccion.setLayout(null);
			panelDireccion.setBounds(new Rectangle(12, 40, 469, 278));
			panelDireccion.add(getPanelDireccion1(), null);
			panelDireccion.add(getPanelContacto(), null);
		}
		return panelDireccion;
	}

	private JPanel getPanelDireccion1() {
		if (panelDireccion1 == null) {
			lblMunicipio = new JLabel();
			lblMunicipio.setBounds(new Rectangle(9, 79, 65, 20));
			lblMunicipio.setText("Municipio:");
			lblMunicipio.setFont(new Font("Arial", Font.PLAIN, 12));
			lblSector = new JLabel();
			lblSector.setBounds(new Rectangle(9, 57, 65, 20));
			lblSector.setText("Sector:");
			lblSector.setFont(new Font("Arial", Font.PLAIN, 12));
			lblNumero = new JLabel();
			lblNumero.setBounds(new Rectangle(9, 35, 65, 20));
			lblNumero.setText("Numero:");
			lblNumero.setFont(new Font("Arial", Font.PLAIN, 12));
			lblCalle = new JLabel();
			lblCalle.setFont(new Font("Arial", Font.PLAIN, 12));
			lblCalle.setBounds(new Rectangle(9, 13, 65, 20));
			lblCalle.setText("Calle:");
			panelDireccion1 = new JPanel();
			panelDireccion1.setLayout(null);
			panelDireccion1.setBounds(new Rectangle(5, 12, 454, 111));
			panelDireccion1.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory
							.createTitledBorder(null, "Direccion",
									TitledBorder.LEFT,
									TitledBorder.DEFAULT_POSITION, new Font(
											"Courier New", Font.PLAIN, 12),
									new Color(51, 51, 51))));
			panelDireccion1.add(lblCalle, null);
			panelDireccion1.add(lblNumero, null);
			panelDireccion1.add(lblSector, null);
			panelDireccion1.add(lblMunicipio, null);
			panelDireccion1.add(getTxtCalle(), null);
			panelDireccion1.add(getTxtNumero(), null);
			panelDireccion1.add(getTxtSector(), null);
			panelDireccion1.add(getTxtMunicipio(), null);
		}
		return panelDireccion1;
	}

	private JTextField getTxtCalle() {
		if (txtCalle == null) {
			txtCalle = new JTextField();
			txtCalle.setBounds(new Rectangle(79, 13, 366, 20));
			txtCalle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			txtCalle.setText(direcciones[0].toString());

		}
		return txtCalle;
	}

	private JTextField getTxtNumero() {
		if (txtNumero == null) {
			txtNumero = new JTextField();
			txtNumero.setBounds(new Rectangle(79, 35, 366, 20));
			txtNumero.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			txtNumero.setText(direcciones[1].toString());
		}
		return txtNumero;
	}

	private JTextField getTxtSector() {
		if (txtSector == null) {
			txtSector = new JTextField();
			txtSector.setBounds(new Rectangle(79, 57, 366, 20));
			txtSector.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			txtSector.setText(direcciones[2].toString());
		}
		return txtSector;
	}

	private JTextField getTxtMunicipio() {
		if (txtMunicipio == null) {
			txtMunicipio = new JTextField();
			txtMunicipio.setBounds(new Rectangle(79, 79, 182, 20));
			txtMunicipio.setBorder(BorderFactory.createLineBorder(Color.BLACK,
					1));
			txtMunicipio.setText(direcciones[3].toString());
		}
		return txtMunicipio;
	}

	private JPanel getPanelContacto() {
		if (panelContacto == null) {
			lblEmail = new JLabel();
			lblEmail.setBounds(new Rectangle(235, 35, 66, 20));
			lblEmail.setText("E-Mail:");
			lblEmail.setFont(new Font("Arial", Font.PLAIN, 12));
			lblTelefono2 = new JLabel();
			lblTelefono2.setBounds(new Rectangle(235, 13, 66, 20));
			lblTelefono2.setText("Telefono2:");
			lblTelefono2.setFont(new Font("Arial", Font.PLAIN, 12));
			lblweb = new JLabel();
			lblweb.setBounds(new Rectangle(9, 57, 66, 20));
			lblweb.setText("Sitio Web:");
			lblweb.setFont(new Font("Arial", Font.PLAIN, 12));
			lblFax = new JLabel();
			lblFax.setBounds(new Rectangle(9, 35, 66, 20));
			lblFax.setText("Fax:");
			lblFax.setFont(new Font("Arial", Font.PLAIN, 12));
			lblTelefono1 = new JLabel();
			lblTelefono1.setBounds(new Rectangle(9, 13, 66, 20));
			lblTelefono1.setText("Telefono1:");
			lblTelefono1.setFont(new Font("Arial", Font.PLAIN, 12));
			panelContacto = new JPanel();
			panelContacto.setLayout(null);
			panelContacto.setBounds(new Rectangle(6, 134, 454, 84));
			panelContacto.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory
							.createTitledBorder(null, "Contactos",
									TitledBorder.LEFT,
									TitledBorder.DEFAULT_POSITION, new Font(
											"Courier New", Font.PLAIN, 12),
									new Color(51, 51, 51))));
			panelContacto.add(lblTelefono1, null);
			panelContacto.add(lblFax, null);
			panelContacto.add(lblweb, null);
			panelContacto.add(getTxtTelefono1(), null);
			panelContacto.add(lblTelefono2, null);
			panelContacto.add(getTxtTelefono2(), null);
			panelContacto.add(getTxtFax(), null);
			panelContacto.add(lblEmail, null);
			panelContacto.add(getTxtEmail(), null);
			panelContacto.add(getTxtWeb(), null);
		}
		return panelContacto;
	}

	private JTextField getTxtTelefono1() {
		if (txtTelefono1 == null) {
			txtTelefono1 = new JTextField();
			txtTelefono1.setBounds(new Rectangle(78, 13, 150, 20));
			txtTelefono1.setBorder(BorderFactory.createLineBorder(Color.BLACK,
					1));
			txtTelefono1.setText(manejador.getColegio().get(0).getTelefono1());
		}
		return txtTelefono1;
	}

	private JTextField getTxtTelefono2() {
		if (txtTelefono2 == null) {
			txtTelefono2 = new JTextField();
			txtTelefono2.setBounds(new Rectangle(303, 13, 144, 19));
			txtTelefono2.setBorder(BorderFactory.createLineBorder(Color.BLACK,
					1));
			txtTelefono2.setText(manejador.getColegio().get(0).getTelefono2());
		}
		return txtTelefono2;
	}

	private JTextField getTxtFax() {
		if (txtFax == null) {
			txtFax = new JTextField();
			txtFax.setBounds(new Rectangle(78, 35, 150, 20));
			txtFax.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			txtFax.setText(manejador.getColegio().get(0).getFax());
		}
		return txtFax;
	}

	private JTextField getTxtEmail() {
		if (txtEmail == null) {
			txtEmail = new JTextField();
			txtEmail.setBounds(new Rectangle(303, 35, 144, 19));
			txtEmail.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			txtEmail.setText(manejador.getColegio().get(0).getEmail());
		}
		return txtEmail;
	}

	private JTextField getTxtWeb() {
		if (txtWeb == null) {
			txtWeb = new JTextField();
			txtWeb.setBounds(new Rectangle(78, 57, 368, 19));
			txtWeb.setText("");
			txtWeb.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			txtWeb.setText(manejador.getColegio().get(0).getWeb());
		}
		return txtWeb;
	}

	private JButton getBtnCancelar() {
		if (btnCancelar == null) {
			btnCancelar = new JButton();
			btnCancelar.setBounds(new Rectangle(392, 339, 85, 26));
			btnCancelar.setFont(new Font("Arial", Font.PLAIN, 12));
			btnCancelar.setText("Cancelar");
			btnCancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DialogoColegio dialogo = DialogoColegio.getInstancia();
					dialogo.dispose();
				}
			});
		}
		return btnCancelar;
	}

	private JButton getBtnAceptar() {
		if (btnAceptar == null) {
			btnAceptar = new JButton();
			btnAceptar.setBounds(new Rectangle(304, 339, 85, 26));
			btnAceptar.setText("Aceptar");
			btnAceptar.setFont(new Font("Arial", Font.PLAIN, 12));
			btnAceptar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					PanelColegio.this.agregarColegio();
				}
			});
		}
		return btnAceptar;
	}

	private JPanel getPanelOtros() {
		if (panelOtros == null) {
			panelOtros = new JPanel();
			panelOtros.setLayout(null);
			panelOtros.setBounds(new Rectangle(12, 40, 469, 278));
			panelOtros.add(getPanelOtros1(), null);
		}
		return panelOtros;
	}

	private JPanel getPanelOtros1() {
		if (panelOtros1 == null) {
			panelOtros1 = new JPanel();
			panelOtros1.setLayout(null);
			panelOtros1.setBounds(new Rectangle(6, 4, 454, 257));
			panelOtros1.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory
							.createTitledBorder(null, "Miscelaneos",
									TitledBorder.LEFT,
									TitledBorder.DEFAULT_POSITION, new Font(
											"Courier New", Font.PLAIN, 12),
									new Color(51, 51, 51))));
			panelOtros1.add(getPanelLogo(), null);
			panelOtros1.add(getBtnLogo(), null);
		}
		return panelOtros1;
	}

	private JPanel getPanelLogo() {
		if (panelLogo == null) {
			panelLogo = new JPanel();
			lblNotas = new JLabel();
			lblNotas.setBounds(new Rectangle(1, 0, 247, 128));
			lblNotas.setFont(new Font("Arial", Font.PLAIN, 12));
			if (manejador.getColegio().get(0).getLogo() != null) {
				lblNotas.setText("");
			} else {

				lblNotas
						.setText("<html>Nota: Se recomienda que utilice una imagen <br> que tenga dimensiones como las del recuadro</html>");
			}
			panelLogo.setLayout(null);
			panelLogo.setBounds(new Rectangle(100, 25, 247, 128));
			panelLogo.setBorder(BorderFactory
					.createEtchedBorder(EtchedBorder.LOWERED));
			panelLogo.add(lblNotas, null);
		}
		return panelLogo;
	}

	private JButton getBtnLogo() {
		if (btnLogo == null) {
			btnLogo = new JButton();
			btnLogo.setBounds(new Rectangle(161, 170, 131, 56));
			btnLogo.setFont(new Font("Arial", Font.PLAIN, 12));
			btnLogo.setIcon(new ImageIcon("./lib/images/openImage.png"));
			btnLogo.setText("<html>Seleccionar Logo</html>");
			btnLogo.setHorizontalTextPosition(JLabel.CENTER);
			btnLogo.setVerticalTextPosition(JLabel.BOTTOM);
			btnLogo.addActionListener(new AttachListener());
		}
		return btnLogo;
	}

	private class AttachListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showDialog(PanelColegio.this, "Aceptar");

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				lblNotas.setText(null);
				file = fc.getSelectedFile();
				cargarImagen(panelLogo, file);
			}
		}
	}

	private void cargarImagen(javax.swing.JPanel jDeskp, File fileImagen) {
		try {
			BufferedImage image = ImageIO.read(fileImagen);
			jDeskp.setBorder(new PintaImagen(image, jDeskp));
		} catch (Exception e) {

		}
	}

	private void agregarColegio() {
		String nombre = txtNombre.getText();
		String calle = txtCalle.getText();
		String numero = txtNumero.getText();
		String sector = txtSector.getText();
		String municipio = txtMunicipio.getText();
		String telefono1 = txtTelefono1.getText();
		String telefono2 = txtTelefono2.getText();
		String fax = txtFax.getText();
		String email = txtEmail.getText();
		String web = txtWeb.getText();
		String logo = file.getAbsolutePath();
		if (Utilidad.isPalabraVacia(nombre, calle, numero, sector, municipio,
				telefono1, logo)) {
			throw new CampoVacioException();
		}

		if (Utilidad.isPalabraVacia(telefono2)) {
			String valor = "N/A";
			txtTelefono2.setText(valor);
		}
		if (Utilidad.isPalabraVacia(fax)) {
			String valor = "N/A";
			txtFax.setText(valor);
		}
		if (Utilidad.isPalabraVacia(email)) {
			String valor = "N/A";
			txtEmail.setText(valor);
		}
		if (Utilidad.isPalabraVacia(web)) {
			String valor = "N/A";
			txtWeb.setText(valor);
		}
		if (Utilidad.isPalabraVacia(logo)) {
			logo = "N/A";
		}
		Colegio colegio = new Colegio(txtNombre.getText(), txtCalle.getText()
				.concat(",")
				+ txtNumero.getText().concat(",")
				+ txtSector.getText().concat(",") + txtMunicipio.getText(),
				txtTelefono1.getText(), txtTelefono2.getText(), txtFax
						.getText(), txtEmail.getText(), txtWeb.getText(), logo);
		manejador.modificarColegio(colegio, manejador.getColegio().get(0)
				.getNombre());
		JOptionPane.showMessageDialog(PanelColegio.this, "Colegio Modificado");
	}

	public void asignarTexto(String idioma, String locale) {
		pestañaNombre = new String[] {
				manejadorDeIdioma.getBundle(idioma, locale).getString(
						"Pestanas_Nombre_0"),
				manejadorDeIdioma.getBundle(idioma, locale).getString(
						"Pestanas_Nombre_1") };
		pestaña.add(pestañaNombre[0], getPanelDireccion());
		pestaña.add(pestañaNombre[1], getPanelOtros());
		lblNombre.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Nombre_Del_Colegio"));
		lblCalle.setText(manejadorDeIdioma.getBundle(idioma, locale).getString(
				"Calle"));
		lblNumero.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Numero"));

		lblSector.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Sector"));
		lblMunicipio.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Municipio"));
		lblTelefono1.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Telefono1"));
		lblTelefono2.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Telefono2"));
		lblFax.setText(manejadorDeIdioma.getBundle(idioma, locale).getString(
				"Fax"));
		lblweb.setText(manejadorDeIdioma.getBundle(idioma, locale).getString(
				"Sitio_Web"));
		lblEmail.setText(manejadorDeIdioma.getBundle(idioma, locale).getString(
				"E-Mail"));

		panelDireccion1.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory
						.createTitledBorder(null, manejadorDeIdioma.getBundle(
								idioma, locale).getString("Direccion"),
								TitledBorder.LEFT,
								TitledBorder.DEFAULT_POSITION, new Font(
										"Courier New", Font.PLAIN, 12),
								new Color(51, 51, 51))));

		panelContacto.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory
						.createTitledBorder(null, manejadorDeIdioma.getBundle(
								idioma, locale).getString("Contactos"),
								TitledBorder.LEFT,
								TitledBorder.DEFAULT_POSITION, new Font(
										"Courier New", Font.PLAIN, 12),
								new Color(51, 51, 51))));

		panelOtros1.setBorder(BorderFactory.createCompoundBorder(BorderFactory
				.createEmptyBorder(0, 0, 0, 0), BorderFactory
				.createTitledBorder(null, manejadorDeIdioma.getBundle(idioma,
						locale).getString("Miscelaneos"), TitledBorder.LEFT,
						TitledBorder.DEFAULT_POSITION, new Font("Courier New",
								Font.PLAIN, 12), new Color(51, 51, 51))));

		btnAceptar.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Aceptar"));
		btnCancelar.setText(manejadorDeIdioma.getBundle(idioma, locale)
				.getString("Cancelar"));

		btnLogo.setText(manejadorDeIdioma.getBundle(idioma, locale).getString(
				"Seleccionar_Logo"));

	}

	public void cambiarIdioma(String idioma, String locale) {
		PanelColegio.this.asignarTexto(idioma, locale);
	}

	/**
	 * This method initializes btnCambiarIdioma
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnCambiarIdioma() {
		if (btnCambiarIdioma == null) {
			btnCambiarIdioma = new JButton();
			btnCambiarIdioma.setBounds(new Rectangle(201, 336, 32, 26));
			btnCambiarIdioma
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							String idioma = "en";
							String locale = "US";
							cambiarIdioma(idioma, locale);

						}
					});
		}
		return btnCambiarIdioma;
	}
}
